package com.xca.znb.view;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xca.znb.R;
import com.xca.znb.contact.ProcurementMessageContact;
import com.xca.znb.entity.Procurement;
import com.xca.znb.model.impl.ProcurementMessageImpl;
import com.xca.znb.presenter.ProcurementMessagePresenter;

public class ProcurementMessage extends AppCompatActivity implements ProcurementMessageContact {
    private ProcurementMessagePresenter presenter ;
    private ImageButton backBtn ;
    private TextView deleteBtn , pType , pVariety , pNum , pUnit , pLocal , pTime , pGetLocal , pText ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_procurement_message);
        initView();
        ProcurementMessageImpl procurementMessage = new ProcurementMessageImpl(this);
        presenter = new ProcurementMessagePresenter(this , procurementMessage);

        Intent intent = getIntent();
        String sId = intent.getStringExtra("pManToMeId");
        String salesPhone = intent.getStringExtra("sales_phone");

        SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
        String phone = sharedPreferences.getString("phone" , "");
        if (!salesPhone.equals(phone)){
            deleteBtn.setVisibility(View.GONE);
        }

        int pId = Integer.parseInt(sId);
        presenter.showMessageById(pId);


        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(ProcurementMessage.this)
                        .setMessage("确定要删除吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                presenter.deleteProcurementById(pId);
                                Procurement procurement = presenter.getProcurementById(pId);
                                if (procurement == null){
                                    Toast.makeText(ProcurementMessage.this, "删除成功", Toast.LENGTH_SHORT).show();
                                    finish();
                                }
                            }
                        })
                        .setNegativeButton("取消",null)
                        .create()
                        .show();
            }
        });
    }

    protected void initView(){
        backBtn = findViewById(R.id.procurement_message_back);
        deleteBtn = findViewById(R.id.procurement_message_delete);
        pType = findViewById(R.id.procurement_message_type) ;
        pVariety = findViewById(R.id.procurement_message_variety);
        pNum = findViewById(R.id.procurement_message_num);
        pUnit = findViewById(R.id.procurement_message_unit);
        pLocal = findViewById(R.id.procurement_message_local);
        pTime = findViewById(R.id.procurement_message_time);
        pGetLocal = findViewById(R.id.procurement_message_getLocal);
        pText = findViewById(R.id.procurement_message_text);
    }

    @Override
    public void getProcurementById(String type, String variety, String num, String unit, String local, String time, String getLocal, String text) {
        pType.setText(type);
        pVariety.setText(variety);
        pNum.setText(num);
        pUnit.setText(unit);
        pLocal.setText(local);
        pTime.setText(time);
        pGetLocal.setText(getLocal);
        pText.setText(text);
    }
}
